package app.util.image.action
{
	import app.gui.control.*;
	import app.util.*;
	
	import flash.display.*;
	import flash.events.*;
	import flash.utils.*;
	
	public class TimeConsumingAction extends AbstractAction
	{		
		private var timerTick:Number;
		
		public function TimeConsumingAction(tick:Number = 1)
		{
			this.timerTick = tick;
		}
		
		public override function getActionedBitmapData():BitmapData
		{
			var timer:Timer = new Timer(this.timerTick, 1);
			var win:ProgressActionWindow = ProgressActionWindow.createInstance();	

			timer.addEventListener(TimerEvent.TIMER_COMPLETE, function(e:TimerEvent):void
			{
				try
				{
					var per:Number = processBitmapDataByStep();
					win.setProgress(per);
					if(per < 1)
					{
						timer.start();
					}
					else
					{
						Mediator.getCurrentImageConfig().pushState(
							getFinalBitmapData(), getDescription());
					}
				}
				catch(e:Error)
				{
					Mediator.handleImageProcessingError(e);
					win.setProgress(1);
				}
			});
			timer.start();
			Mediator.getLogger().log("Start a progressive action timer");

			return null;
		}
		
		/**
		 * Return the percentage(0 - 1) when the process is not finally done.
		 */
		protected virtual function processBitmapDataByStep():Number
		{
			throw new Error("Unimplemented TimeConsumingAction");
		} 
		
		protected virtual function getFinalBitmapData():BitmapData
		{
			throw new Error("Unimplemented TimeConsumingAction");
		}
	}
}